import request, {BASEURL} from '@/utils/request'

// 创建订单
export const createOrder = (data) => request.post('/order/submit', data)

// 获取订单列表
export const getOrderList = (params) => request.get('/order/userPage', { params })

// 获取订单列表（管理员）
export const getOrderListByAdmin = (params) => request.get('/order/adminPage', { params })

// 获取订单详情
export const getOrderDetail = (id) => request.get(`/order/detail/${id}`)

// 取消订单
export const cancelOrder = (id) => request.put(`/order/cancel/${id}`)

// 确认收货
export const confirmOrder = (id) => request.put(`/order/receive/${id}`)

// 支付
export const payOrderApi = (id) => request.put(`/order/pay/${id}`)

//派送
export const dispatchOrder = (id) => request.put(`/order/dispatch/${id}`)

//标记订单已支付
export const markOrderAsPaid = (id) => request.put(`/order/markAsPaid/${id}`)

/**
 * 导出订单数据
 * @param {Object} params - 查询参数
 */
import axios from 'axios';
import { getToken } from '@/utils/auth';
export function exportOrdersApi(params) {
    const token = getToken();
    const baseUrl = BASEURL || '';

    // 使用axios发起请求，可以携带请求头
    return axios({
        url: `${baseUrl}/order/export`,
        method: 'get',
        params: params,
        headers: {
            'Authorization': `Bearer${token}` // 根据您的后端要求调整token格式
        },
        responseType: 'blob' // 指定响应类型为二进制数据
    }).then(response => {
        // 创建Blob对象
        const blob = new Blob([response.data]);
        // 创建下载链接
        const url = window.URL.createObjectURL(blob);
        // 创建a标签并模拟点击下载
        const link = document.createElement('a');
        link.href = url;
        link.download = `订单数据_${new Date().getTime()}.xlsx`; // 设置下载文件名
        document.body.appendChild(link);
        link.click();
        // 清理
        window.URL.revokeObjectURL(url);
        document.body.removeChild(link);

        return { code: '200', msg: '导出成功' };
    });
}

//批量取消订单
export const batchCancelOrders = (orderIds) => request.put("/order/batchCancel", orderIds )